<?php

function insertRecipe($title,$preparation,$image,$date) {
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if($dbLink) {
		$title = mysql_real_escape_string($title);
		$preparation = mysql_real_escape_string($preparation);
		$image = mysql_real_escape_string($image);
		$result_query = mysql_query("select * from recipe where title like '$title'");
		if (mysql_num_rows($result_query) == 0) {
			$result = mysql_query("insert into recipe (title,preparation,image,date)
			 	values ('$title','$preparation','$image','$date')",$dbLink);
			if (!$result) {
				transactionClose();
				redirectError("recipe not inserted");
			}
			else {
				$recipeId = mysql_insert_id($dbLink);
				transactionClose();
				return $recipeId;
			}
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}
} // end insertRecipe


function findRecipeByRecipeId($recipeId){
	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if (($recipeId > 0) && ($dbLink)){
		$result_query = mysql_query("select * from recipe where recipeId = $recipeId");
		transactionClose();
		if (mysql_num_rows($result_query) == 1) {
			$recipe = mysql_fetch_assoc($result_query);
			return $recipe;
		}
		else{	
			redirectError("problems to get recipe information");
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}

}// end findRecipeByRecipeId


function findRecipesByLabelId($labelId){

	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if (($labelId > 0) && ($dbLink)){
		$result_query = mysql_query("select * from recipe, `recipe-label` where
												`recipe-label`.labelId = $labelId
												and
												recipe.recipeId = `recipe-label`.recipeId order by recipe.title");
		transactionClose();
		if (mysql_num_rows($result_query) > 0) {
			$count = mysql_num_rows($result_query);
			for ($i=0;$i<$count;$i++)
				$result2[$i]= mysql_fetch_assoc($result_query);
			return $result2;
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}
} // end findRecipesByLabelId



function findRecipes(){

	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if ($dbLink){
		$result_query = mysql_query("select * from recipe order by title");
		transactionClose();
		if (mysql_num_rows($result_query) >= 1) {
			$count = mysql_num_rows($result_query);
			for ($i=0;$i<$count;$i++)
				$result2[$i]= mysql_fetch_assoc($result_query);
			return $result2;
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}

} // end findRecipesByLabelId


function findLastRecipes(){

	transactionOpen();
	$dbLink = $GLOBALS["dbLink"];
	if ($dbLink){
		$result_query = mysql_query("select recipeId, title, preparation, image, UNIX_TIMESTAMP(date) as date from recipe order by date desc");
		transactionClose();
		if (mysql_num_rows($result_query) >= 1) {
			$count = mysql_num_rows($result_query);
			for ($i=0;$i<$count;$i++)
				$result2[$i]= mysql_fetch_assoc($result_query);
			return $result2;
		}
	}
	else {
		transactionClose();
		redirectError("problems with data base connection");
	}

} // end findLastRecipesByLabelId

?>